---
import { getCollection, getEntry } from 'astro:content';
import Layout from '../layouts/Layout.astro';
import { DocsNavFromLocalStorage } from '@ag-website-shared/components/docs-navigation/DocsNavFromLocalStorage';
import { getDocsPages } from '../components/docs/utils/pageData';
import { parseVersion } from '@ag-website-shared/utils/parseVersion';
import { SideNavigation } from '@components/pages-navigation/components/SideNavigation';
import MajorTable from '@ag-website-shared/components/major-table/MajorTable.astro';
import docsStyles from '@ag-website-shared/components/page-styles/docs.module.scss';

import type { CollectionEntry } from 'astro:content';

export async function getStaticPaths() {
    const pages = await getCollection('docs');
    return getDocsPages(pages);
}

const { data: docsNavData } = (await getEntry('docsNav', 'nav')) as CollectionEntry<'docsNav'>;

const versionsContent = await getEntry('versions', 'ag-grid-versions');
const versionsData: any[] = versionsContent ? versionsContent.data : [];

const majors = versionsData.reduce((majors, version) => {
    if (parseVersion(version.version).isMajor) {
        majors.push(parseVersion(version.version).major);
    }

    return majors;
}, []);

const headings = [
    { slug: 'top', depth: 1, text: 'Documentation Archive' },
    ...majors.map((major) => {
        return { slug: `version-${major}`, depth: 2, text: `Version ${major}` };
    }),
];
---

<Layout
    title={'AG Grid Documentation Archive'}
    description={'Browse archived documentation for previous AG Grid versions, from version 14 and onwards. View changelogs for every minor and major release.'}
    showDocsNav={true}
    showSearchBar={true}
>
    <div class:list={[docsStyles.contentViewport, 'layout-grid']}>
        <DocsNavFromLocalStorage client:load menuData={docsNavData} pageName={'documentation-archive'} />

        <div id="doc-content" class={docsStyles.docPage}>
            <h1>Documentation Archive</h1>

            <div class={docsStyles.pageSections}>
                <article>
                    <p>Review documentation for previous AG Grid versions.</p>

                    {
                        majors.map((major) => {
                            return (
                                <>
                                    <h2 id={`version-${major}`}>Version {major}</h2>
                                    <MajorTable library="grid" major={major} type="archive" />
                                </>
                            );
                        })
                    }
                </article>
            </div>
        </div>

        <SideNavigation client:load headings={headings} delayedScrollSpy={true} />
    </div>
</Layout>
